﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Level_Editor
{
    class Obstacle : Actor
    {
        public struct Circle
        {
            public int[] x, y;
            public int r, m;

            public Circle(int x, int y, int r)
            {
                this.x = new int[12];
                this.y = new int[12];

                this.x[0] = x;
                this.y[0] = y;
                this.r = r;

                m = 0;

                for (int i = 1; i < 12; i++)
                {
                    this.x[i] = (int)Math.Round(Math.Cos(Math.PI * -i / 6) * this.x[0] - Math.Sin(Math.PI * -i / 6) * this.y[0]);
                    this.y[i] = (int)Math.Round(Math.Sin(Math.PI * -i / 6) * this.x[0] + Math.Cos(Math.PI * -i / 6) * this.y[0]);

                    m = Math.Max(Math.Abs(this.x[i]), m);
                    m = Math.Max(Math.Abs(this.y[i]), m);
                }
            }
        }

        public struct Line
        {
            public int[] x1, y1, x2, y2;
            public int m;

            public Line(int x1, int y1, int x2, int y2)
            {
                this.x1 = new int[12];
                this.y1 = new int[12];
                this.x2 = new int[12];
                this.y2 = new int[12];

                this.x1[0] = x1;
                this.y1[0] = y1;
                this.x2[0] = x2;
                this.y2[0] = y2;

                m = 0;

                for (int i = 1; i < 12; i++)
                {
                    this.x1[i] = (int)Math.Round(Math.Cos(Math.PI * -i / 6) * this.x1[0] - Math.Sin(Math.PI * -i / 6) * this.y1[0]);
                    this.y1[i] = (int)Math.Round(Math.Sin(Math.PI * -i / 6) * this.x1[0] + Math.Cos(Math.PI * -i / 6) * this.y1[0]);

                    m = Math.Max(Math.Abs(this.x1[i]), m);
                    m = Math.Max(Math.Abs(this.y1[i]), m);
                }

                for (int i = 1; i < 12; i++)
                {
                    this.x2[i] = (int)Math.Round(Math.Cos(Math.PI * -i / 6) * this.x2[0] - Math.Sin(Math.PI * -i / 6) * this.y2[0]);
                    this.y2[i] = (int)Math.Round(Math.Sin(Math.PI * -i / 6) * this.x2[0] + Math.Cos(Math.PI * -i / 6) * this.y2[0]);

                    m = Math.Max(Math.Abs(this.x2[i]), m);
                    m = Math.Max(Math.Abs(this.y2[i]), m);
                }
            }
        }

        public List<Circle> circles;
        public List<Line> lines;
        public int center_x, center_y;

        public Obstacle(string self, string file, string name, ref StreamReader myStream)
            : base(self, file, name, "0")
        {
            center_x = Convert.ToInt32(myStream.ReadLine());
            center_y = Convert.ToInt32(myStream.ReadLine());

            circles = new List<Circle>();
            lines = new List<Line>();
            int size = 0;

            int circleCount = Convert.ToInt32(myStream.ReadLine());

            for (int i = 0; i < circleCount; i++)
            {
                int x = Convert.ToInt32(myStream.ReadLine());
                int y = Convert.ToInt32(myStream.ReadLine());
                int r = Convert.ToInt32(myStream.ReadLine());

                Circle temp = new Circle(x, y, r);

                if (temp.m > size)
                    size = temp.m;

                circles.Add(temp);
            }

            int lineCount = Convert.ToInt32(myStream.ReadLine());

            for (int i = 0; i < lineCount; i++)
            {
                int x1 = Convert.ToInt32(myStream.ReadLine());
                int y1 = Convert.ToInt32(myStream.ReadLine());
                int x2 = Convert.ToInt32(myStream.ReadLine());
                int y2 = Convert.ToInt32(myStream.ReadLine());

                Line temp = new Line(x1, y1, x2, y2);

                if (temp.m > size)
                    size = temp.m;
                
                lines.Add(temp);
            }

            size = size + Math.Max(center_x, center_y);

            if (size == 0)
                size = 1;

            type = 3;

            genActors(this.collision, 0, 0, size);
        }
    }
}
